This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
In this portfolio I was very interested in maps again. I used a training I found online on how to make maps. I went through the training first (it gave direct code for different things) and then tried to extend what I learned onto new data sets.
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
library(raster)
## Loading required package: sp
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
##
## intersect, select, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(spData)
library(spDataLarge)
## Warning: package 'spDataLarge' was built under R version 4.1.3
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ stringr 1.4.0
## ✓ tidyr 1.1.4 ✓ forcats 0.5.1
## ✓ readr 2.1.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x tidyr::extract() masks raster::extract()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x dplyr::select() masks raster::select()
library(tmap) # for static and interactive maps
library(leaflet) # for interactive maps
library(ggplot2)
library(mapview)
# Add fill layer to nz shape
tm_shape(nz) +
tm_fill()
# Add border layer to nz shape
tm_shape(nz) +
tm_borders()
# Add fill and border layers to nz shape
tm_shape(nz) +
tm_fill() +
tm_borders()
map_nz = tm_shape(nz) + tm_polygons()
class(map_nz)
## [1] "tmap"
#> [1] "tmap"
map_nz1 = map_nz +
tm_shape(nz_elev) + tm_raster(alpha = 0.7)
map_nz1
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
urb_1970_2030 = urban_agglomerations %>%
filter(year %in% c(1970, 1990, 2010, 2030))
tm_shape(world) +
tm_polygons() +
tm_shape(urb_1970_2030) +
tm_symbols(col = "black", border.col = "white", size = "population_millions") +
tm_facets(by = "year", nrow = 2, free.coords = FALSE)
For some reason this gif did not work. I can’t figure out where I am going wrong.
urb_anim = tm_shape(world) +
tm_polygons() +
tm_shape(urb_1970_2030) +
tm_symbols(col = "black", border.col = "white", size = "population_millions") +
tm_facets(along = "year", free.coords = FALSE)
tmap_animation(urb_anim, filename = "urb_anim2.gif", width = 5 , height = 5 , delay = 25)
## Creating frames
## ========================================
## ====================
## ====================
##
## Creating animation
## Animation saved to /Users/elaynaseago/Downloads/R_Class/portfolio-10/urb_anim2.gif
urb_anim = tm_shape(world) + tm_polygons() +
tm_shape(urban_agglomerations) + tm_dots(size = "population_millions") +
tm_facets(along = "year", free.coords = FALSE)
tmap_animation(urb_anim, filename = "urb_anim.gif", delay = 25)
## Creating frames
## =========
## ====
## =====
## ====
## =====
## ====
## =====
## ====
## ====
## =====
## ====
## =====
## ====
## =====
## ====
## =====
## ====
##
## Creating animation
## Animation saved to /Users/elaynaseago/Downloads/R_Class/portfolio-10/urb_anim.gif
tmap_mode("view")
## tmap mode set to interactive viewing
map_nz
mapview::mapview(nz)
library(mapdeck)
##
## Attaching package: 'mapdeck'
## The following object is masked from 'package:tibble':
##
## add_column
set_token(Sys.getenv("MAPBOX"))
crash_data = read.csv("https://git.io/geocompr-mapdeck")
crash_data = na.omit(crash_data)
ms = mapdeck_style("dark")
mapdeck(style = ms, pitch = 45, location = c(0, 52), zoom = 4) %>%
add_grid(data = crash_data, lat = "lat", lon = "lng", cell_size = 1000,
elevation_scale = 50, layer_id = "grid_layer",
colour_range = viridisLite::plasma(6))
## Registered S3 method overwritten by 'jsonify':
## method from
## print.json jsonlite